package com.smasher.weight.helper;

import android.content.Context;
import android.media.MediaDrm;
import android.media.UnsupportedSchemeException;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;

import com.smasher.weight.util.MacAddressUtil;

import java.util.Arrays;
import java.util.UUID;

/**
 * @author smasher
 */
public class MacAddressHelper {

    public static final String TAG = "MacAddress";

    private String mac;

    private MacAddressHelper() {
    }

    private static class SingletonInstance {
        private static final MacAddressHelper INSTANCE = new MacAddressHelper();
    }

    public static MacAddressHelper getInstance() {
        return SingletonInstance.INSTANCE;
    }


    public void init(Context context) {
        try {
            UUID wideVineUuid = new UUID(-0x121074568629b532L, -0x5c37d8232ae2de13L);
            MediaDrm wvDrm = new MediaDrm(wideVineUuid);
            byte[] wideVineId = wvDrm.getPropertyByteArray(MediaDrm.PROPERTY_DEVICE_UNIQUE_ID);
            Log.d(TAG, "wideVineId: " + Arrays.toString(wideVineId));
        } catch (UnsupportedSchemeException e) {
            e.printStackTrace();
        }

        String uniqueId = UUID.randomUUID().toString();
        Log.d(TAG, "UUID: " + uniqueId);

        String serial = Build.SERIAL;
        Log.d(TAG, "SERIAL: " + serial);
        String equipmentCode = "";
        String temp = MacAddressUtil.getMac(context);
        if (temp != null) {
            equipmentCode = temp.replace(":", "").trim().toUpperCase();
        }
        Log.d("Unique", "mac address: " + equipmentCode);
        mac = equipmentCode;
    }


    public String getMacAddress(Context context) {
        if (TextUtils.isEmpty(mac)) {
            init(context);
        }
        return mac;
    }

}